package ipebg.mzk.provider.system.service.impl;

import ipebg.mzk.facade.entity.MsAdminFunction;
import ipebg.mzk.facade.entity.MsAdminUser;
import ipebg.mzk.facade.entity.MsAdminUserRole;
import ipebg.mzk.facade.entity.MsFunctionOption;
import ipebg.mzk.facade.entity.MsFunctionRoleOption;
import ipebg.mzk.facade.system.service.MsFunctionRoleOptionService;
import ipebg.mzk.provider.system.dao.AdminRoleDao;
import ipebg.mzk.provider.system.dao.AdminUserDao;
import ipebg.mzk.provider.system.dao.AdminUserRoleDao;
import ipebg.mzk.provider.system.dao.MsFunctionRoleOptionDao;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("msFunctionRoleOptionServiceImpl")
public class MsFunctionRoleOptionServiceImpl implements MsFunctionRoleOptionService{
	
	@Resource(name="msFunctionRoleOptionDao")
	MsFunctionRoleOptionDao msFunctionRoleOptionDao;
	
	@Resource(name="adminUserDao")
	AdminUserDao adminUserDao;
	
	@Resource(name="adminUserRoleDao")
	AdminUserRoleDao adminUserRoleDao;
	
	@Resource(name="adminRoleDao")
	AdminRoleDao adminRoleDao;

	
	@Transactional
	@Override
	public void saveEntity(MsFunctionRoleOption roleOptionBean) throws Exception{
		try{
			msFunctionRoleOptionDao.saveOrUpdate(roleOptionBean);
		}catch (Exception e) {
			throw e;
		}
	}
	
	@Transactional
	public void deleteEntity(String functionId,String roleId) throws Exception{
		try{
			String select="from MsFunctionRoleOption where functionId='"+functionId+"' " +
					" and roleId='"+roleId+"'";
			List<MsFunctionRoleOption> deBeanList=msFunctionRoleOptionDao.queryForList(select, null);
			msFunctionRoleOptionDao.deleteAll(deBeanList);
		}catch (Exception e) {
			throw e;
		}
	}


	@Override
	public MsFunctionRoleOption findEntity(MsFunctionOption bean,String roleId) throws Exception {
		MsFunctionRoleOption oBean =null;
		try{
			String select="from MsFunctionRoleOption where functionId='"+bean.getFunctionId()+"' " +
						" and roleId='"+roleId+"' and optionId='"+bean.getOptionId()+"'";
			oBean= msFunctionRoleOptionDao.findUnique(MsFunctionRoleOption.class,select);
		}catch (Exception e) {
			throw e;
		}
		return oBean;
	}


	@Override
	public String returnOptionByUserFunctionName(String userNo,String functionName) throws Exception {
		String Options="";
		try{
			//獲取用戶實體，進而使用adminId
			String selectHql="from MsAdminUser where loginName='"+userNo+"'";
			MsAdminUser user= adminUserDao.findUnique(MsAdminUser.class,selectHql);
			
			//獲取功能項實體，進而使用functionId
			selectHql="from MsAdminFunction where functionName='"+functionName+"'";
			MsAdminFunction functionBean=adminRoleDao.findUnique(MsAdminFunction.class,selectHql);
			
			//獲取userRole列表
			selectHql="from MsAdminUserRole where adminId='"+user.getAdminId()+"'";
			List<MsAdminUserRole> userRoleBeanList=adminUserRoleDao.queryForList(selectHql, null);
			if(userRoleBeanList != null){
				for(MsAdminUserRole userRoleBean:userRoleBeanList){
					selectHql="from MsFunctionRoleOption where functionId='"+functionBean.getFunctionId()+"'" +
							" and roleId='"+userRoleBean.getRoleId()+"'";
					List<MsFunctionRoleOption> optionList=msFunctionRoleOptionDao.queryForList(selectHql, null);
					if(optionList != null){
						for(MsFunctionRoleOption optionBean:optionList){
							Options+=optionBean.getOptionId()+";";
						}
					}
				}	
			}
		}catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return Options;
	}


	@Transactional
	@SuppressWarnings("unchecked")
	@Override
	public List<MsFunctionRoleOption> findOptionsByFunctionIdRoleId(String functionId, String roleId) throws Exception {
		List<MsFunctionRoleOption> list =null;
		try{
			String select="from MsFunctionRoleOption where functionId='"+functionId+"' " +
						" and roleId='"+roleId+"'";
			list=(List<MsFunctionRoleOption>) msFunctionRoleOptionDao.queryForList(select, null);
		}catch (Exception e) {
			throw e;
		}
		return list;
	}
} 
